package com.example.chenjun.myapplication.net.okhttp.request;

import android.support.annotation.NonNull;

import com.example.chenjun.myapplication.net.okhttp.OkHttpLoader;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.Request;
import okhttp3.RequestBody;

public abstract class OkHttpRequest {
    Map<String, String> params;
    protected Map<String, String> headers;
    protected String url;
    private String tag;
    private String method;

    public OkHttpRequest(Map<String, String> params,Map<String, String> headers,@NonNull String url, String tag, String method){
        this.params = params;
        this.headers = headers;
        this.url = url;
        this.tag = tag;
        this.method = method;
        if (this.params == null){
            this.params = new HashMap<>();
        }
        if (this.headers == null){
            this.headers = new HashMap<>();
        }

    }

    protected abstract RequestBody buildRequestBody();

    public Request buildRequest(){
        Request.Builder builder = new Request.Builder();
        Set<String> headerKetSet = headers.keySet();
        for (String headerKey : headerKetSet) {
            builder.addHeader(headerKey, headers.get(headerKey));
        }
        builder.method(method, buildRequestBody());
        builder.url(url).tag(tag);
        return builder.build();
    }
}
